﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace STSdb.MTL.Utils.Serialization
{
    public static class SerializationExtensions
    {
        /// <summary>
        /// Serializes object to XML string
        /// </summary>
        /// <param name="objectForSerialization"></param>
        /// <returns></returns>
        public static string SerializeXml(this object objectForSerialization)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(objectForSerialization.GetType());

                string r = String.Empty;

                using (System.IO.StringWriter wr = new System.IO.StringWriter())
                {
                    xs.Serialize(wr, objectForSerialization);
                    r = wr.GetStringBuilder().ToString();
                    wr.Close();
                }

                return r;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            
        }

        /// <summary>
        /// Deserializes object from XML string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeserializeXml<T>(this string str)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));

                object r = null;
                using (System.IO.StringReader sr = new System.IO.StringReader(str))
                {
                    r = xs.Deserialize(new System.IO.StringReader(str));
                    sr.Close();
                }

                return (T)r;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            
        }

        public static object DeserializeXml(this string str,Type type)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);

                object r = null;
                using (System.IO.StringReader sr = new System.IO.StringReader(str))
                {
                    r = xs.Deserialize(new System.IO.StringReader(str));
                    sr.Close();
                }

                return r;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
